/*
Package requests
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/4/28 10:01
@File     : pagination_request.go
@Project  : gohub
@Desc     : 分页query参数校验
*/
package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type PaginationRequest struct {
	Sort    string `json:"sort" form:"sort" valid:"sort"`
	Order   string `json:"order" form:"order" valid:"order"`
	PerPage string `json:"per_page" form:"per_page" valid:"per_page"`
}

func Pagination(data any, c *gin.Context) map[string][]string {
	//校验规则
	rules := govalidator.MapData{
		"sort":     []string{"in:id,created_at,updated_at"},
		"order":    []string{"in:asc,desc"},
		"per_page": []string{"numeric_between:1,100"},
	}
	//错误提示
	messages := govalidator.MapData{
		"sort":     []string{"in:排序字段仅支持id,created_at,updated_at"},
		"order":    []string{"in:排序方式仅支持asc,desc"},
		"per_page": []string{"numeric_between:每页显示条数仅支持1-100"},
	}
	return validate(data, rules, messages)
}
